package com.example.tbag.healthcare.Activity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.EditText;
import android.widget.TextView;

import com.example.tbag.healthcare.Model.Cache;
import com.example.tbag.healthcare.R;
import com.example.tbag.healthcare.Util.CallPhoneUtil;
import com.example.tbag.healthcare.Util.DatabaseHelper;
import com.example.tbag.healthcare.Util.ToastUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by tbag on 2017/5/2.
 */

public class AddMedecineActivity extends AppCompatActivity {
    private boolean isShow = false;
    private int id;
    @BindViews({R.id.edit1, R.id.edit2})
    List<EditText> editTexts;
    @BindView(R.id.text)
    TextView textView;
    private DatabaseHelper helper;

    @OnClick(R.id.btn_back)
    public void back() {
        this.finish();
    }

    @OnClick(R.id.btn1)
    public void exit() {
        this.finish();
    }

    @OnClick(R.id.right_btn)
    public void callForHelp() {
        CallPhoneUtil.call(this);
    }

    @OnClick(R.id.btn2)
    public void checkAndSave() {
        int index = 0;
        ContentValues cv = new ContentValues();
        for (EditText editText : editTexts) {
            String text = editText.getText().toString().trim();
            switch (index) {
                case 0:
                    if (text.isEmpty()) {
                        ToastUtil.show(this, "药品名称不能为空");
                        return;
                    }
                    cv.put("name", text);
                    break;
                case 1:
                    if (text.isEmpty()) {
                        ToastUtil.show(this, "药品价格不能为空");
                        return;
                    }
                    cv.put("money", text);
                    break;
            }
            index++;
        }
        if(isShow){
            helper.getWritableDatabase().update("med",cv,"_id = ?",new String[]{id+""});
            ToastUtil.show(this, "修改成功");
        }else {
            helper.getWritableDatabase().insert("med", null, cv);
            ToastUtil.show(this, "保存成功");
        }
        new Handler(getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                exit();
            }
        }, 800);
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_med_info);
        ButterKnife.bind(this);
        id = getIntent().getIntExtra("id",0);
        if(getIntent().getIntExtra("type",1)==0){
            isShow = true;
            textView.setText("修改药品信息");
            change();
        }
        helper = new DatabaseHelper(this);
    }

    public Cache getPerson() {
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.query("med",null,"_id =?",new String[]{id+""},null,null,null);
        if (cursor != null && cursor.moveToFirst()) {
            Cache humanInfo = getHumanInfo(cursor);
            cursor.close();
            db.close();
            return humanInfo;
        }
        ToastUtil.show(this,"系统出现问题");
        finish();
        return null;
    }

    public Cache getHumanInfo(Cursor cursor) {
        String date = cursor.getString(cursor.getColumnIndex("name"));
        String time = cursor.getString(cursor.getColumnIndex("money"));
        int  id = cursor.getInt(cursor.getColumnIndex("_id"));
        Cache cache = new Cache(date, time,id);
        return cache;
    }

    private void change() {
        Cache humanInfo = getPerson();
        editTexts.get(0).setText(humanInfo.title);
        editTexts.get(1).setText(humanInfo.money);
    }
}
